package com.ja.openchat.ui.me;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.zy.openchat.App;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.zy.openchat.core.model.group.SuperGroupFullInfo;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.core.model.user.UserInfo;
import com.ja.openchat.databinding.FragmentQrCodeBinding;
import com.zy.openchat.util.ScreenViewUtil;
import com.zy.openchat.util.share.GoShare;
import com.ja.openchat.viewmodels.QrCodeModel;
import com.zhihu.matisse.internal.utils.SingleMediaScanner;

import net.glxn.qrgen.android.QRCode;

import java.io.FileNotFoundException;

import me.goldze.mvvmhabit.base.BaseFragment;

//我的二维码
public class QrCodeFragment extends BaseFragment<FragmentQrCodeBinding, QrCodeModel> {


    private SuperGroupFullInfo super_groupFullInfo;
    private ChatInfo chatInfo;

    @Override
    public void initParam() {
        super.initParam();
        Bundle bundle = getArguments();
        if(bundle != null){
            if(bundle.getParcelable("super_groupFullInfo") != null){
                chatInfo = bundle.getParcelable("chatInfo");
                super_groupFullInfo = bundle.getParcelable("super_groupFullInfo");
            }
        }

    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_qr_code;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initView() {
        super.initView();
        BarUtils.setStatusBarColor(getActivity(), ColorStateList.valueOf(Color.parseColor("#0ad59f")).getDefaultColor());
    }

    @Override
    public void initData() {
        super.initData();
        if(super_groupFullInfo != null){
            binding.headerImageView.showGroup(chatInfo, (int) getResources().getDimension(R.dimen.x70));
            binding.titleBar.setTitle(getString(R.string.group_qr_code));
            binding.qrDesc.setText(String.format(getString(R.string.group_qr_tip),getString(R.string.app_name)));
            viewModel.userName.set(chatInfo.title);
            viewModel. myBitmap.set(QRCode.from(super_groupFullInfo.invite_link).withSize(800, 800).bitmap());
            viewModel.genrateLink(chatInfo);
        }else{
            binding.headerImageView.showUser(UserInfo.getUserInfo(), (int) getResources().getDimension(R.dimen.x70));
        }

    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onQrCodeSaveEvent.observe(this, aBoolean -> {
            doSaveOrShare(0);
        });
        viewModel.uc.onQrCodeShareEvent.observe(this, aBoolean -> {
            doSaveOrShare(1);
        });
    }

    private String qrFilePath = null;

    private void doSaveOrShare(int position) {
//        String filePath = QRCode.from(UserInfo.getUserInfo().qrString()).withSize(800, 800).file().getAbsolutePath();
        if (TextUtils.isEmpty(qrFilePath)) {
            doSaveView();
            qrFilePath = ScreenViewUtil.getFileByView(getContext(), binding.flQrContent);
        }
        if (TextUtils.isEmpty(qrFilePath)) {
            initReplayView();
            return;
        }
        if (position == 1) {
            new GoShare.FShareBuilder(getActivity(), App.getInstanse().getPackageName() + ".utilcode.fileprovider")
                    .setFilePath(qrFilePath)
                    .setShareSubject(getResources().getString(R.string.app_name))
                    .build()
                    .shareByCustom();

        } else {
            try {
                MediaStore.Images.Media.insertImage(getContext().getContentResolver(), qrFilePath, getResources().getString(R.string.app_name), getResources().getString(R.string.app_name));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            new SingleMediaScanner(getContext(), qrFilePath, new SingleMediaScanner.ScanListener() {
                @Override
                public void onScanFinish() {
                    ToastUtils.showShort(getString(R.string.save_album_success));
                }
            });
        }
        initReplayView();
    }

    private void doSaveView(){
        binding.llLogo.setVisibility(View.VISIBLE);
        binding.tvSave.setVisibility(View.GONE);
        binding.tvShare.setVisibility(View.GONE);
        binding.qrDesc.setTextColor(getResources().getColor(R.color.black));
    }

    private void initReplayView(){
        binding.llLogo.setVisibility(View.INVISIBLE);
        binding.tvSave.setVisibility(View.VISIBLE);
        binding.tvShare.setVisibility(View.VISIBLE);
        binding.qrDesc.setTextColor(getResources().getColor(R.color.color_bdbdbd));
    }

}